<?php
namespace app\api\controller;
use think\Controller;
use think\Db;

class Api extends Controller
{
    //获取茶的种类
    function getTeaCase()
    {
        $retval = model('Api')->getTeaCase();
        // print_pre($retval);
        if(count($retval)==0){
            return json_encode(0);
        }

        return json_encode($retval);
    }

    //获取茶产区
    function getTeaArea($id)
    {
        $retval = model('Api')->getTeaArea($id);
        if(count($retval)==0){
            return json_encode(0);
        }
        return json_encode($retval);
    }


    //获取榜单
    function getBangDan($teacase_id, $teaarea_id, $bangdan_id){
        $retval = model('Api')->getBangDan($teacase_id, $teaarea_id, $bangdan_id);
        if(count($retval)==0){
            return json_encode(0);
        }
        return json_encode($retval);
    }

    //获取来源
    function source($teacase_id,$teaarea_id)
    {
        $source = db('case_area')->field('source')->where(['teacase_id'=>$teacase_id,'teaarea_id'=>$teaarea_id])->find();
        return json_encode($source);
    }


    //获取匠人或珍品或品牌的详细信息
    function getDetail($id,$bangdan_id,$teaarea_id='',$teacase_id='',$locations="0",$openid="")
    {
        $retval = model('Api')->getDetail($id, $bangdan_id);
        if($openid){
            if($locations==1){
                select_click($id, $bangdan_id,$teaarea_id,$teacase_id,$openid,$locations,365,30); 
            }else{
                select_click($id, $bangdan_id,$teaarea_id,$teacase_id,$openid,$locations,20,10);
            }
            // 增加点击有效量
        }
        if(count($retval)==0){
            return json_encode(0);
        }
        return json_encode($retval);
    }

    // 是否存在openid字段
    function getUser($openid){
        $resa = db('user')->where('openID',$openid)->find();
        if(!$resa||!$resa['avatarUrl']&&!$resa['nickName']){
            return json_encode(0);   
        }else{
            return json_encode(1);
        }
    }

    //版权说明
    function getB_content(){
        $res = model('Api')->findcopyright();
        if(count($res)==0){
            return json_encode(0);
        }
        return json_encode($res);
    }

    // 搜索
    function search_like_name($name=""){
            $array = [];
            $v1  = db('jiangren')->where("name","like","%$name%")->field('bangdan_id id,id sid,teacase_id tid,teaarea_id hid,name')->select();
            $v2  = db('pinpai')->where("name","like","%$name%")->field('bangdan_id id,id sid,teacase_id tid,teaarea_id hid,name')->select();
            $v3  = db('zhenpin')->where("name","like","%$name%")->field('bangdan_id id,id sid,teacase_id tid,teaarea_id hid,name')->select();

            $n1 = db('teaarea')->alias('t')->where('area',"like","%$name%")->join('case_area ca','t.id=ca.teaarea_id','left')->join('teacase tc','ca.teacase_id=tc.id')->field('t.id hid,t.area bdname,tc.id tid, tc.teaname name,t.area tarea')->select();

            if($v1){
                foreach($v1 as &$item){
                    $ranking = ranking($item['id'],$item['tid'],$item['hid'],$item['sid']);
                    $item['ins'] = $ranking;
                    $area = db('teaarea')->find($item['hid']);
                    $item['areaname'] =  $area['area'];
                    $item['bdname'] = list_ct_search($item['id']);
                }
                $array = array_merge($array,$v1);
                // $array[] = $v1;
            }
            if($v2){
                foreach($v2 as &$item){
                    $ranking = ranking($item['id'],$item['tid'],$item['hid'],$item['sid']);
                    $item['ins'] = $ranking;
                    $area = db('teaarea')->find($item['hid']);
                    $item['areaname'] =  $area['area'];
                    $item['bdname'] = list_ct_search($item['id']);
                }
                $array = array_merge($array,$v2);
                // $array[] = $v2;
            }
            if($v3){
                foreach($v3 as &$item){
                    $ranking = ranking($item['id'],$item['tid'],$item['hid'],$item['sid']);
                    $item['ins'] = $ranking;
                    $area = db('teaarea')->find($item['hid']);
                    $item['areaname'] =  $area['area'];
                    $item['bdname'] = list_ct_search($item['id']);
                }
                $array = array_merge($array,$v3);
                // $array[] = $v3;
            }
            if($n1){
                $array = array_merge($array,$n1);
                // $array[] = $n1;
            }
            if(!$v1&&!$v2&&!$v3&&!$n1){
                return null;
            }
            // mp($array);
            return json_encode($array);

        }

     // 标题显示产区-茶类
    function serach_tea($teaarea_id,$teacase_id){
        $tc = db('teacase')->find($teacase_id);
        $ta = db('teaarea')->find($teaarea_id);
        $mername = $ta['area']."-".$tc['teaname'].'排行榜';
        return json_encode($mername);
    } 

     //搜索所有热门搜索
     function hot_search_ct($teacase_id="0"){
        $str = '';
        $list = db('Hot_search')->where('list','neq',0)->where('head',$teacase_id)->order('list asc')->select();
        $lists = 3-count($list);
        if(count($list)>0){
            foreach ($list as $listes) {
                $str += $listes['Id'] . ",";
            }
            $str = rtrim($str, ',');
            // $res = db('Hot_search')->where('Id','neq',$str)->limit($lists)->order('frequency desc')->select();
            $sql = "select * from Hot_search where Id not in (".$str .") and list = 0 and teacase_id =".$teacase_id." order by frequency desc  limit ".$lists;
            $res = Db::query($sql);
            $res = array_merge($res,$list);
        }else{
            $res = db('Hot_search')->where('list','neq',0)->where('head',$teacase_id)->limit($lists)->order('list asc')->select();
        }
        foreach ($res as &$item) {
           if($item['bangdan_id']==1){
            $item['bdname'] = '匠人榜';
           }else if($item['bangdan_id']==2){
            $item['bdname'] = '品牌榜';
           }else if($item['bangdan_id']==3){
            $item['bdname'] = '珍品榜';
           }else{
            $item['bdname'] = '未上榜';
           }
            if($item['teaarea_id']!=0){
                $name_item = db('teaarea')->find($item['teaarea_id']);
                $item['name'] = $name_item['area'];
            }else{
                $name_item = db('teacase')->find($item['teacase_id']);
                $item['name'] = $name_item['teaname'];
            }
        }
        if($res){
           return json_encode($res); 
        }
        return json_encode(0);
     }

    // 词条是否存在
    function search_ct($name)
    {
            $v1  = db('jiangren')->where("name","$name")->select();
            $v2  = db('pinpai')->where("name","$name")->select();
            $v3  = db('zhenpin')->where("name","$name")->select();
        if($v1||$v2||$v3){
            return 1;
        }else{
            return 0;
        }


    }




}


?>